<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>树结构展示</title>
  <script src="./treeData.js"></script>
  <style>
    .node-item {
      padding-left: 15px;
    }
  </style>
</head>

<body>
  <input id="searchName" type="text" value=""> <button onclick="searchTreeFn()">树搜索</button>
  <div id="tree-wrap" class="tree-wrap">
    <h3>树结构渲染</h3>
  </div>
  </div>

  <script>
    let appendString = ""
    let nodeIndex = 0; // 节点位置和顺序
    // 树结构渲染 - 采用递归
    loadTree(treeData.module, 1)
    function loadTree(target = {}, deepLev = 1) {
      if (!!target.children && !!target.children.length) {
        // 有子元素的时候, 塞头塞尾; 自身再循环
        appendString += `<div class="node-item">${deepLev} - ${nodeIndex} - ${target.nodeName}`
        target.children.forEach((item, index) => {
          nodeIndex++
          if (!!item.children && item.children.length) {
            let nextLevel = deepLev + 1
            loadTree(item, nextLevel)
          } else {
            appendString += `<div class="node-item">${deepLev + 1} - ${nodeIndex} - ${item.nodeName}</div>`
          }
        });
        appendString += '</div>'
      } else {
        appendString += `<div class="node-item">${deepLev} - ${nodeIndex} - ${target.nodeName}</div>`
      }
    }
    var treeWrap = document.getElementById('tree-wrap')
    treeWrap.innerHTML += appendString


    // 树搜索
    function searchTreeFn() {
      // var inputData = document.getElementById('searchName').value;
      let inputData = "tb_agen"
      const rebuildData = (value, arr = []) => {
        let newarr = [];
        arr.forEach(element => {
          if (element.children && element.children.length) {
            const ab = rebuildData(value, element.children);
            const obj = {
              ...element,
              children: ab
            };
            if (ab && ab.length) {
              newarr.push(obj);
            }
          } else {
            // console.log(element.nodeName)
            if (element.nodeName.indexOf(value) > -1) {
              newarr.push(element);
            }
          }
        });
        return newarr;
      };
      console.log(rebuildData(inputData, treeData.module.children))
      // let newData = {}
      // function searchTree(target = {}, parentNode = {}) {
      //   if (!!target.children && !!target.children.length) {
      //     target.children.forEach((item, index) => {
      //       if (!!item.children && item.children.length) {
      //         // let nextLevel = deepLev + 1
      //         searchTree(item, target)
      //       } else {
      //         let findText = item.nodeName.indexOf(inputData);
      //         if (findText != -1) {
      //           console.log(item)
      //         }
      //       }
      //     });
      //   } else {
      //     let findText = target.nodeName.indexOf(inputData);
      //     if (findText != -1) {
      //       console.log(target)
      //     }
      //   }
      // }
      // searchTree(treeData.module, "")
      // console.log(newData)
    }
  </script>
</body>

</html>